package com.platform.controller;

import com.platform.entity.LotteryConfigEntity;
import com.platform.entity.LotteryEntity;
import com.platform.entity.LotterySignEntity;
import com.platform.service.LotteryConfigService;
import com.platform.service.LotteryRecordService;
import com.platform.service.LotteryService;
import com.platform.service.LotterySignService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.math.BigDecimal.ROUND_HALF_DOWN;

/**
 * 抽奖配置Controller
 *
 * @author liweihai
 * @email liweihai12345@sina.com
 * @date 2019-04-16 19:25:00
 */
@RestController
@RequestMapping("lotteryconfig")
public class LotteryConfigController {
    @Autowired
    private LotteryConfigService lotteryConfigService;
    @Autowired
    private LotteryRecordService lotteryRecordService;
    @Autowired
    private LotteryService lotteryService;
    @Autowired
    private LotterySignService lotterySignService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("lotteryconfig:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<LotteryConfigEntity> lotteryConfigList = lotteryConfigService.queryList(query);
        int total = lotteryConfigService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(lotteryConfigList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 签到打卡日开奖
     */
    @RequestMapping("/opensignbyday")
    public R opensignbyday() {
        Map<String, Object> map = new HashMap<>();
        map.put("isyestoday", 1);
        List<LotterySignEntity>  lotterySignEntitys= lotterySignService.queryList(map);
        if(lotterySignEntitys.size()<=0)
            return R.ok("无开奖记录！");
        BigDecimal nosignmoney = new BigDecimal(0);
        Integer signcount = 0;
        for (LotterySignEntity lotterySignEntity : lotterySignEntitys) {
            if(lotterySignEntity.getStatus()==0)
                nosignmoney = nosignmoney.add(lotterySignEntity.getSpend());
            else if(lotterySignEntity.getStatus()==1)
                signcount++;
        }
        for (LotterySignEntity lotterySignEntity : lotterySignEntitys) {
            if(lotterySignEntity.getStatus()==0) {
                lotterySignEntity.setStatus(2);
                lotterySignService.update(lotterySignEntity);
            }
            else if(lotterySignEntity.getStatus()==1)
            {
                map = new HashMap<>();
                map.put("level", 1);
                map.put("type", 4);
                List<LotteryEntity> le = lotteryService.queryList(map);
                if(le==null&&le.size()<=0)
                    continue;
                LotteryEntity lotteryEntity = le.get(0);
                BigDecimal price = new BigDecimal(0);
                if(nosignmoney.compareTo(new BigDecimal(0))<=0)
                    price=lotterySignEntity.getSpend();
                else
                    price=lotterySignEntity.getSpend().add(nosignmoney.multiply(lotteryEntity.getPrice()).divide(new BigDecimal(signcount),2,ROUND_HALF_DOWN));
                lotteryRecordService.savesignrecordinfo(lotteryEntity,lotterySignEntity.getUid(),price,lotterySignEntity.getSpend());
            }
        }
        return R.ok();
    }

    /**
     * 签到打卡周开奖
     */
    @RequestMapping("/opensignbyweek")
    public R opensignbyweek() {
        Map<String, Object> map = new HashMap<>();
        map.put("isweekly", 1);
        List<LotterySignEntity>  lotterySignEntitys= lotterySignService.queryList(map);
        if(lotterySignEntitys==null||lotterySignEntitys.size()<=0)
            return R.ok("无开奖记录！");
        BigDecimal nosignmoney = new BigDecimal(0);

        for (LotterySignEntity lotterySignEntity : lotterySignEntitys) {
            if(lotterySignEntity.getStatus()==2)
                nosignmoney = nosignmoney.add(lotterySignEntity.getSpend());
        }
        map = new HashMap<>();
        map.put("isweeklysign", 1);
        List<LotterySignEntity>  weeklylotterySigns= lotterySignService.queryList(map);
        if(weeklylotterySigns==null||weeklylotterySigns.size()<=0)
            return R.ok("无开奖记录！");
        List<Long> userIds = weeklylotterySigns.stream().map(u -> u.getUid()).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
        for (Long uid : userIds) {
            map = new HashMap<>();
            map.put("level", 2);//1日奖励，2周奖励
            map.put("type", 4);
            List<LotteryEntity> le = lotteryService.queryList(map);
            if (le == null && le.size() <= 0)
                continue;
            LotteryEntity lotteryEntity = le.get(0);
            BigDecimal price = new BigDecimal(0);
            if (nosignmoney.compareTo(new BigDecimal(0)) > 0) {
                price = nosignmoney.multiply(lotteryEntity.getPrice()).divide(new BigDecimal(userIds.size()),2,ROUND_HALF_DOWN);
                lotteryRecordService.savesignrecordinfo(lotteryEntity, uid, price, new BigDecimal(10));
            }
        }
        return R.ok();
    }
    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("lotteryconfig:info")
    public R info(@PathVariable("id") Integer id) {
        LotteryConfigEntity lotteryConfig = lotteryConfigService.queryObject(id);

        return R.ok().put("lotteryConfig", lotteryConfig);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("lotteryconfig:save")
    public R save(@RequestBody LotteryConfigEntity lotteryConfig) {
        lotteryConfig.setCreateDate(new Date());
        lotteryConfigService.save(lotteryConfig);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("lotteryconfig:update")
    public R update(@RequestBody LotteryConfigEntity lotteryConfig) {
        lotteryConfigService.update(lotteryConfig);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("lotteryconfig:delete")
    public R delete(@RequestBody Integer[] ids) {
        lotteryConfigService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<LotteryConfigEntity> list = lotteryConfigService.queryList(params);

        return R.ok().put("list", list);
    }


    @RequestMapping("queryAllLottery")
    public R queryAllLottery(){
        List<LotteryConfigEntity> list = lotteryConfigService.queryAllLottery();
        return R.ok().put("list",list);
    }
}
